package com.aloda.controller;

import com.aloda.anno.OperationLog;
import com.aloda.pojo.dto.UserAddDTO;
import com.aloda.pojo.dto.UserEditDTO;
import com.aloda.pojo.dto.UserLoginDTO;
import com.aloda.pojo.dto.UserPageDTO;
import com.aloda.pojo.vo.UserLoginVO;
import com.aloda.pojo.vo.UserPageVO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户controller层
 *
 * @author aloda
 * @createTime 2024-11-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userLoginDTO 登录dto
     * @return vo
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO, request);
        return Result.success("登录成功",userLoginVO);
    }

    /**
     * 用户退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        //销毁Session
        session.invalidate();
        return Result.success("退出成功");
    }

    /**
     * 分页查询用户
     * @param userPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<UserPageVO>> getUserPage(@RequestBody UserPageDTO userPageDTO) {
        PageResult<UserPageVO> result = userService.getUserPage(userPageDTO);
        return Result.success(result);
    }

    /**
     * 新增用户
     * @param userAddDTO
     * @return
     */
    @OperationLog
    @PostMapping
    public Result addUser(@RequestBody UserAddDTO userAddDTO) {
        userService.addUser(userAddDTO);
        return Result.success();
    }

    /**
     * 修改用户状态
     * @param status
     * @return
     */
    @PostMapping("/status/{status}/{id}")
    public Result setUserStatus(@PathVariable String status, @PathVariable Integer id) {
        userService.setUserStatus(status,id);
        return Result.success();
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<UserPageVO> getUserInfo(@PathVariable Integer id) {
        UserPageVO userPageVO = userService.getUserInfo(id);
        return Result.success(userPageVO);
    }

    /**
     * 编辑用户信息
     * @param userEditDTO
     * @return
     */
    @OperationLog
    @PutMapping
    public Result editUser(@RequestBody UserEditDTO userEditDTO) {
        userService.editUser(userEditDTO);
        return Result.success();
    }
}
